package com.yonyou.chuan.servlet;

import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.json.JSONUtil;
import com.yonyou.chuan.constant.Constant;
import com.yonyou.chuan.crypto.EncryptionHolder;
import com.yonyou.chuan.crypto.EventCrypto;
import com.yonyou.chuan.crypto.PrivateAppCrypto;
import com.yonyou.chuan.model.param.NxQxgParam;
import com.yonyou.chuan.model.subscribe.EventContent;
import com.yonyou.chuan.model.param.YonBipParam;
import com.yonyou.chuan.service.MessageService;
import com.yonyou.chuan.log.LoggerUtil;
import nc.bs.framework.adaptor.IHttpServletAdaptor;
import nc.bs.framework.common.NCLocator;
import nc.bs.framework.server.ISecurityTokenCallback;
import nc.itf.uap.busibean.ISysInitQry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class YhtEventServlet implements IHttpServletAdaptor {

    @Override
    public void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            ISysInitQry sysInit = NCLocator.getInstance().lookup(ISysInitQry.class);
            MessageService messageService = NCLocator.getInstance().lookup(MessageService.class);
            NCLocator.getInstance().lookup(ISecurityTokenCallback.class).token("NCSystem".getBytes(),"pfxx".getBytes());
            //TODO 业务参数注入
            NxQxgParam nxQxgParam = new NxQxgParam();
            YonBipParam yonPublicParam = new YonBipParam();

            String bip_appKey = sysInit.getParaString(Constant.GLOBE_ORG, "bip_appKey");    //bip应用参数
            String bip_secret = sysInit.getParaString(Constant.GLOBE_ORG, "bip_secret");    //bip应用参数
            String ncc_origin = sysInit.getParaString(Constant.GLOBE_ORG, "ncc_origin");    //7小哥业务中心地址

            String qxg_origin = sysInit.getParaString(Constant.GLOBE_ORG, "qxg_origin");    //7小哥业务中心地址
            String VerifyPath = sysInit.getParaString(Constant.GLOBE_ORG, "qxg_verify_internet");    //7小哥认证中心地址外网
            String qxg_appKey = sysInit.getParaString(Constant.GLOBE_ORG, "qxg_appKey");    //bip应用参数
            String qxg_secret = sysInit.getParaString(Constant.GLOBE_ORG, "qxg_secret");    //bip应用参数

            nxQxgParam.setQxg_verify(VerifyPath);
            nxQxgParam.setQxg_appKey(qxg_appKey);
            nxQxgParam.setQxg_secret(qxg_secret);
            nxQxgParam.setQxg_origin(qxg_origin);

            yonPublicParam.setNcc_origin(ncc_origin);
            yonPublicParam.setBip_appKey(bip_appKey);
            yonPublicParam.setBip_appSecret(bip_secret);

            //TODO 解析请求报文
            String body = ServletUtil.getBody(request);
            EncryptionHolder holder = JSONUtil.toBean(body, EncryptionHolder.class);
            // 构建解密验签处理对象
            EventCrypto crypto = PrivateAppCrypto.newCrypto(bip_appKey, bip_secret);
            // 验签解密后的消息体
            String decrypt = crypto.decryptMsg(holder);
            // 反序列化后的消息内容对象
            EventContent event = JSONUtil.toBean(decrypt, EventContent.class);
            // 根据事件类型进行相关处理操作，耗时操作推荐异步处理，平台默认推送超时时间为 5 秒，超时视为推送失败
            LoggerUtil.printLogger("【解析事件中心的消息】 租户: {} ,事件类型: {} ,事件ID: {},事件内容: {} ",event.getTenantId(), event.getType(), event.getEventId(),event.getContent());
            switch (event.getType()) {
                case TODO_CENTER_ADD_TODO:
                    messageService.sendTodoMessage(event.getContent(), nxQxgParam, yonPublicParam);
                    break;
                case TODO_CENTER_UPDATE_TODO:
                    messageService.updateTodoMessage(event.getContent(), nxQxgParam);
                    break;
                case WORK_NOTIFY_WORK_NOTIFY:
                    messageService.sendWorkNotify(event.getTenantId(),event.getContent(), nxQxgParam);
                    break;
                case WORK_NOTIFY_ALARM_NOTIFY:
                    messageService.sendAlarmNotify(event.getTenantId(),event.getContent(), nxQxgParam);
                    break;
                case UNKNOWN:
                    LoggerUtil.printLogger("未知事件");
                    break;
            }
            response.getWriter().write("success");
        } catch (Exception e) {
            LoggerUtil.printLogger(e.getMessage());
            response.getWriter().write("error");
        }finally {
            response.getWriter().close();
        }
    }
}
